<%@ page import="org.apache.commons.logging.Log" %>
<%@ page import="org.apache.commons.logging.LogFactory" %>
<%@ page import="za.org.foobar.famon.model.*" %>
<%@ page import="za.org.foobar.famon.simpleui.PageUtil" %>
<%@ page import="za.org.foobar.famon.simpleui.SimpleUiUtil" %>
<%@ page import="java.util.List" %>
<%@ page import="za.org.foobar.famon.util.ServerUtil" %>
<%@ page import="za.org.foobar.famon.webapp.UserLogic" %>
<%@ page import="za.org.foobar.famon.webapp.FailureLogic" %>
<%@ page import="za.org.foobar.famon.util.DB" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%
    Log log = LogFactory.getLog("za.org.foobar.famon.web.queue.jsp");
    FailureLogic failureLogic = FailureLogic.get();
    UserLogic userLogic = UserLogic.get();
    if (!userLogic.isUserAdmin()) {
        response.sendRedirect("/");
        return;
    }

    String paramRetryAll = ServerUtil.urlDecode(request.getParameter("retryAll"));
    String paramDeleteAll = ServerUtil.urlDecode(request.getParameter("deleteAll"));
    String paramShowFailureId = ServerUtil.urlDecode(request.getParameter("showFailureId"));

    PageUtil pageUtil = new PageUtil(request);

    StringBuilder error = new StringBuilder();

    List<FailureQueue> failureQueue = failureLogic.getFailureQueue();
    if (paramRetryAll != null) {
        for (FailureQueue q : failureQueue) {
            if (q.isFailed()) {
                q.setFailed(false);
            }
        }
    } else if (paramDeleteAll != null) {
        for (FailureQueue q : failureQueue) {
            DB.remove(q);
        }
        DB.closePM();
        failureQueue = failureLogic.getFailureQueue();
    } else {
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            if (name.startsWith("retryId") && name.length() > 7) {
                Key k = FailureQueue.stringToKey(name.substring(7));
                FailureQueue queue = DB.find(FailureQueue.class, k);
                if (queue != null) {
                    queue.setFailed(false);
                }
            }
        }
        DB.closePM();
        failureQueue = failureLogic.getFailureQueue();
    }

%>
<html>
<head>
    <title>Failure</title>
    <link rel="stylesheet" href="simpleui.css"/>
    <link rel="stylesheet" href="bluedream.css"/>
</head>
<body>
<%=SimpleUiUtil.renderLoginHeader(request)%>
<div class="breadCrumb">
    <a href="index.jsp">Failures</a>
</div>
<div class="body">
<%
    if (error.length() > 0) {
        out.print(error.toString());
    } else {
        %>
        <form action="queue.jsp" method="post">
            <table width="100%">
                <caption>Failure Queue</caption>
                <thead>
                    <tr>
                        <th>Date Received</th>
                        <th>Failed</th>
                        <th>Source Address</th>
                        <th>Source Hostname</th>
                        <th>Source Port</th>
                        <th>Retry</th>
                    </tr>
                </thead>
                <tbody>
                <%
                    {
                        failureQueue = pageUtil.getResultList(failureQueue);
                        pageUtil.printPagesTr(out, 6);
                        boolean odd = true;
                        for (FailureQueue queue : failureQueue) {
                            String id = queue.keyToString();
                            %><tr<%=odd ? " class=\"odd\"" : "" %>>
                                <td><a href="queue.jsp?showFailureId=<%=id%>"><%=queue.getDateReceived()%></a></td>
                                <td><%=SimpleUiUtil.toString(queue.isFailed())%></td>
                                <td><%=queue.getSourceAddress()%></td>
                                <td><%=queue.getSourceHostname()%></td>
                                <td><%=queue.getSourcePort()%></td>
                                <td><%
                                    if (queue.isFailed()) {
                                        %><input type="checkbox" name="retryId<%=queue.keyToString()%>"><%
                                    }
                                %></td>
                            </tr><%
                            if (id.equals(paramShowFailureId)) {
                                %><tr<%=odd ? " class=\"odd\"" : "" %>><td colspan="6"><pre><%=queue.getJson().getValue().replace("{", "\n{")%></pre></td></tr><%
                            }
                            odd = !odd;
                        }
                        pageUtil.printPagesTr(out, 6);
                    }
                %>
                </tbody>
            </table>
            <label>Retry All <input type="checkbox" name="retryAll"/></label><br>
            <label>Delete All <input type="checkbox" name="deleteAll"/></label><br>
            <input type="submit" value="Go">
        </form>
    <%
    }
%>
</div>

</body>
</html>
<%
    DB.closePM();
%>